<template>
  <el-dialog
    :title="!dataForm.replyId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    width="30%">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="问题名称： " prop="name" label-width="100px">
        <el-input type="text" placeholder="请输入内容限制20字以内" v-model.trim="dataForm.name" maxlength="20" show-word-limit style="width:400px"/>
      </el-form-item>
      <el-form-item label="问题类型: " label-width="100px" prop="typeId">
        <el-select v-model="dataForm.typeId" placeholder="请选择">
          <el-option
            v-for="(item,index) in this.typeList"
            :key="item.typeId"
            :label="item.typeName"
            :value="item.typeId" />
        </el-select>
      </el-form-item>
      <el-form-item label="类别排序" prop="sort" label-width="100px">
        <el-input type="number" v-model.trim="dataForm.sort" placeholder="限制数字" style="width:120px" />
      </el-form-item>
      <!--<el-form-item label="问题回复： " prop="content" label-width="100px">
        <el-input type="textarea" :rows="6" placeholder="请输入问题回复" v-model="dataForm.content" style="width:400px" />
      </el-form-item>-->
      <el-form-item label="备注： " prop="remarks" label-width="100px">
        <el-input type="textarea" :rows="6" placeholder="备注" v-model="dataForm.remarks" style="width:400px" />
      </el-form-item>
      <div>
        <h2 >问题详情</h2>
      </div>
      <UEditor ref="ueditor"></UEditor>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import UEditor from '@/components/ueditor/ueditor.vue'
  export default {
    data () {
      return {
        visible: false,
        dialogImageUrl: '',
        dialogVisible: false,
        typeList: [],
        dataForm: {
          replyId: '',
          typeId: '',
          name: '',
          sort: '',
          content: '',
          remarks: ''
        },
        dataRule: {
          name: [
            { required: true, message: '问题名称不能为空', trigger: 'blur' }
          ],
          content: [
            { required: true, message: '问题回复不能为空', trigger: 'blur' }
          ],
          sort: [
            { required: true, message: '类别排序不能为空', trigger: 'blur' },
            { pattern: /^[0-9]*[1-9][0-9]*$/, message: '类别排序为正整数', trigger: 'blur' }
          ],
          remarks: [
            { required: false, message: '类别排序不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    components: {
      UEditor
    },
    methods: {
      init (id) {
        this.dataForm.replyId = id || 0
        this.visible = true
        debugger
        this.$refs.ueditor.setUEContent('')
        // 请求类型
        this.$http({
          url: this.$http.adornUrl('/replyType/queryReplyTypeList'),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.typeList = data.data.list
          }
        })
        this.$nextTick(() => {
          if (this.$refs['dataForm'] !== undefined) {
            this.$refs['dataForm'].resetFields()
          }
          if (this.dataForm.replyId) {
            this.$http({
              url: this.$http.adornUrl(`/reply/getReplyById/${this.dataForm.replyId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.typeId = data.data.typeId
                this.dataForm.replyId = data.data.replyId
                this.dataForm.name = data.data.name
                this.dataForm.sort = data.data.sort
                this.$refs.ueditor.setUEContent(data.data.content)
                this.dataForm.remarks = data.data.remarks
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        console.log(this.dataForm.replyId)
        console.log(this.dataForm.typeId)
        debugger
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.typeId === '' || this.dataForm.typeId == null) {
              this.$message.error('请选择类型')
              return
            }
            this.$http({
              url: this.$http.adornUrl(`/reply/${!this.dataForm.replyId ? 'addNewReply' : 'modifyReplyById'}`),
              method: 'post',
              data: this.$http.adornData({
                'replyId': this.dataForm.replyId || undefined,
                'typeId': this.dataForm.typeId,
                'name': this.dataForm.name,
                'sort': this.dataForm.sort,
                'content': this.$refs.ueditor.getUEContent(),
                'remarks': this.dataForm.remarks
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
